﻿using Cmsp3.Common.Domains;
using Demo.PetaPoco.Application.DbContexts;
using Microsoft.Extensions.DependencyInjection;
using PetaPoco;
using PmSoft.Cache.Abstractions;
using PmSoft.Core;
using PmSoft.Data.PetaPoco.Repositories;

namespace Demo.PetaPoco.Application.Services.User;

[Inject(ServiceLifetime.Scoped)]
public class RoleMenuRepository : CacheRepository<AppDbContext, SysRoleMenu, int>
{
	public RoleMenuRepository(AppDbContext dbContext, IApplicationContext applicationContext) : base(dbContext, applicationContext)
	{
	}

	public async Task<IEnumerable<SysRoleMenu>> GetRoleMenusAsync(int roleId)
	{
		int areaVersion = await CacheVersionService.GetAreaVersionAsync(m => m.RoleId, roleId);
		return await GetTopEntitiesWithCacheAsync(1000,
			CachingExpirationType.UsualObjectCollection,
			() => $"RoleMenus:{roleId}:{areaVersion}",
			() =>
		{
			Sql sql = Sql.Builder.Select("id").From("sys_role_menu").Where("role_id=@0", roleId);
			return sql;
		});
	}
}
